package com.hxkj.service.userCenter.impl;

import com.github.pagehelper.PageInfo;
import com.hxkj.client.service.UserManagementServiceClient;
import com.hxkj.domain.po.AuthUser;
import com.hxkj.domain.po.SysUser;
import com.hxkj.service.FunctionService;
import com.hxkj.service.userCenter.UserManagementService;
import com.hxkj.token.utils.TokenUtils;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import com.platform.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
@Slf4j
public class UserManagementServiceImpl implements UserManagementService {

    @Autowired
    private UserManagementServiceClient umClient;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private FunctionService functionService;

    @Override
    public PageInfo<SysUser> getList(SysUser user, PageTo pageParam) throws Exception {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        ParameterDataStruct<SysUser> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(user);
        parameterDataStruct.setPageTo(pageParam);
        //获取当前登录用户信息
        AuthUser authUser = (AuthUser) tokenUtils.getTokenUserInfo();
        //获取当前登录用户的权限集合
        Set<String> functions = functionService.findPermissionsByAccount(authUser.getUserAccount());
        PageInfo<SysUser> result = umClient.getList(parameterDataStruct);
        //如果没有操作用户中心-用户列表数据的权限，屏蔽用户的手机号数据
        if(!functions.contains("sysUser:operation")){
            for (SysUser sysUser : result.getList()) {
                sysUser.setPhone(null);
            }
        }
        return result;
    }

    @Override
    public SysUser getUser(SysUser user) throws Exception {
        if(StringUtils.isBlank(user.getUserId())){
            throw new CustomMessageException("用户编号不能为空");
        }
        ResultDataStruct<SysUser> result = umClient.getUser(user);
        if(result.getCode()!=200){
            throw new CustomMessageException(result.getMessage());
        }
        return result.getData();
    }

    @Override
    public void modifyUserStatus(SysUser user) throws Exception {
        if(StringUtils.isBlank(user.getUserId())){
            throw new CustomMessageException("用户编号不能为空");
        }
        if(StringUtils.isBlank(user.getDelTag())){
            throw new CustomMessageException("用户状态不能为空");
        }
        user.setUpdateTime(System.currentTimeMillis());
        ResultMessageStruct result = umClient.modifyUserStatus(user);
        if(result.getCode()!=200){
            throw new CustomMessageException(result.getMessage());
        }
    }

    @Override
    public void modifyUser(SysUser user) throws Exception {
        if(StringUtils.isBlank(user.getUserId())){
            throw new CustomMessageException("用户编号不能为空");
        }
        if(StringUtils.isBlank(user.getUserAccount())){
            throw new CustomMessageException("用户名不能为空");
        }
        user.setUpdateTime(System.currentTimeMillis());
        ResultMessageStruct result = umClient.modifyUser(user);
        if(result.getCode()!=200){
            throw new CustomMessageException(result.getMessage());
        }
    }
}

